#######################################
######Masculinity Threat Cleaning######
#######################################
###############10/10/25##################
#######################################

####Cleaning Script for Gothreau & Haas, forthcoming in Journal of Experimental Political Science###
####A Replication and Extension of Willer et al. (2013) Overdoing Gender: A Test of the Masculine Overcompensation Thesis### 

library(haven)
library(ggplot2)
library(dplyr)
library(broom)
library(officer)
library(flextable)
library(forcats)
library(psych)
library(survey)
library(readr)

#Set Working Directory#
MTData <- read_dta('[9948.159] TESS 159 Gothreau_FinalWtd.dta')

#Set gender and experimental conditions as factors 
MTData$P_EXP <- factor(MTData$P_EXP, levels=c("1", "2", "3", "4"), labels = c("threat", "no threat", "constrained", "PCKT"))
MTData$gender_binary <- factor(MTData$Q_CDGDRDESC_17, levels = c("1", "2"), labels = c("Men", "Women"))

table(MTData$P_EXP, MTData$gender_binary)

#Recode DVs- rescale 0-1
#Coded such that higher values=more conservative response 

MTData$IRAQ_WAR[MTData$IRAQ_WAR == 98] <- NA
MTData$IRAQ_WAR_rescaled <- (MTData$IRAQ_WAR - 1) / 6

MTData$GAY_RIGHT[MTData$GAY_RIGHT == 98] <- NA
MTData$GAY_RIGHT_rev <- 8 - MTData$GAY_RIGHT
MTData$GAY_RIGHT_rescaled <- (MTData$GAY_RIGHT_rev - 1) / 6

MTData$SUV_DESIRABILITY[MTData$SUV_DESIRABILITY == 98] <- NA
MTData$SUV_DESIRABILITY_rescaled <- (MTData$SUV_DESIRABILITY - 1) / 9

MTData$SYSTEM_JUSTIFICATION[MTData$SYSTEM_JUSTIFICATION == 98] <- NA
MTData$SYSTEM_JUSTIFICATION_rescaled <- (MTData$SYSTEM_JUSTIFICATION - 1) / 9

MTData$DOMINANCE1[MTData$DOMINANCE1 == 98] <- NA
MTData$DOMINANCE1_rescaled <- (MTData$DOMINANCE1 - 1) / 9

MTData$DOMINANCE2[MTData$DOMINANCE2 == 98] <- NA
MTData$DOMINANCE2_rescaled <- (MTData$DOMINANCE2 - 1) / 9

MTData$DOMINANCE_scale <- rowMeans(
  MTData[, c("DOMINANCE1_rescaled", "DOMINANCE2_rescaled")],
  na.rm = TRUE
)

MTData$TRADITIONALISM1[MTData$TRADITIONALISM1 == 98] <- NA
MTData$TRADITIONALISM1_rescaled <- (MTData$TRADITIONALISM1 - 1) / 8

MTData$TRADITIONALISM2[MTData$TRADITIONALISM2 == 98] <- NA
MTData$TRADITIONALISM2_rescaled <- (MTData$TRADITIONALISM2 - 1) / 8

MTData$TRADITIONALISM_scale <- rowMeans(
  MTData[, c("TRADITIONALISM1_rescaled", "TRADITIONALISM2_rescaled")],
  na.rm = TRUE
)

MTData$IMMIGRATION[MTData$IMMIGRATION == 98] <- NA
MTData$IMMIGRATION_rescaled <- (MTData$IMMIGRATION - 1) / 6

MTData$MARIJUANA[MTData$MARIJUANA == 98] <- NA
MTData$MARIJUANA_rev <- 8 - MTData$MARIJUANA
MTData$MARIJUANA_rescaled <- (MTData$MARIJUANA_rev - 1) / 6

MTData$ELECTRIC_CAR[MTData$ELECTRIC_CAR == 98] <- NA
MTData$ELECTRIC_CAR_rev <- 8 - MTData$ELECTRIC_CAR
MTData$ELECTRIC_CAR_rescaled <- (MTData$ELECTRIC_CAR_rev - 1) / 6

MTData$TRANS_RIGHT[MTData$TRANS_RIGHT == 98] <- NA
MTData$TRANS_RIGHT_rev <- 11 - MTData$TRANS_RIGHT
MTData$TRANS_RIGHT_rescaled <- (MTData$TRANS_RIGHT_rev - 1) / 9

MTData$HIRINGPOLICY_WOMEN[MTData$HIRINGPOLICY_WOMEN == 98] <- NA
MTData$HIRINGPOLICY_WOMEN_rev <- 8 - MTData$HIRINGPOLICY_WOMEN
MTData$HIRINGPOLICY_WOMEN_rescaled <- (MTData$HIRINGPOLICY_WOMEN_rev - 1) / 6

MTData$CANCEL_CULTURE[MTData$CANCEL_CULTURE == 98] <- NA
MTData$CANCEL_CULTURE_rescaled <- (MTData$CANCEL_CULTURE - 1) / 6

MTData$THREAT_MALE[MTData$THREAT_MALE == 98] <- NA
MTData$THREAT_MALE_rescaled <- (MTData$THREAT_MALE - 1) / 3

MTData$THREAT_FEMALE[MTData$THREAT_FEMALE == 98] <- NA
MTData$THREAT_FEMALE_rescaled <- (MTData$THREAT_FEMALE - 1) / 3

MTData$CONSERVATISM[MTData$CONSERVATISM == 98] <- NA
MTData$CONSERVATISM_rescaled <- NA  # initialize the new variable
MTData$CONSERVATISM_rescaled[MTData$CONSERVATISM == 1] <- 0
MTData$CONSERVATISM_rescaled[MTData$CONSERVATISM == 2] <- 2
MTData$CONSERVATISM_rescaled[MTData$CONSERVATISM == 3] <- 1

MTData$SUV_PAY[MTData$SUV_PAY == 9999998] <- NA
MTData$SUV_PAY_rescaled <- (MTData$SUV_PAY - min(MTData$SUV_PAY, na.rm = TRUE)) /
  (max(MTData$SUV_PAY, na.rm = TRUE) - min(MTData$SUV_PAY, na.rm = TRUE))

# Composite for Cultural Conservatism (Factor 1)
MTData$Cultural_Conservatism <- rowMeans(MTData[, c(
  "GAY_RIGHT_rescaled", 
  "ELECTRIC_CAR_rescaled", 
  "TRANS_RIGHT_rescaled", 
  "HIRINGPOLICY_WOMEN_rescaled", 
  "MARIJUANA_rescaled", 
  "CONSERVATISM_rescaled"
)], na.rm = TRUE)

# Composite for Hierarchical Thinking (Factor 2)
MTData$Hierarchical_Thinking <- rowMeans(MTData[, c(
  "DOMINANCE_scale", 
  "TRADITIONALISM_scale", 
  "SYSTEM_JUSTIFICATION_rescaled", 
  "IMMIGRATION_rescaled", 
  "IRAQ_WAR_rescaled"
)], na.rm = TRUE)


#Export clean data file as .csv
write_csv(MTData, "CleanMTData.csv")
